package com.cheil.publish.config;

import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserContext;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Playwright;
import jakarta.annotation.PreDestroy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 @author ssk
 @date 2025/9/8
 **/
@Configuration
public class PlaywrightConfig {

    private Playwright playwright;
    private BrowserContext browserContext;

    @Bean
    public BrowserContext context() {
        playwright = Playwright.create();
        Browser  browser = playwright.chromium()
                .launch(new BrowserType.LaunchOptions()
                        .setHeadless(false));
        browserContext = browser.newContext();
        return browserContext;
    }
    @PreDestroy
    public void close() {
        if (browserContext != null) {
            browserContext.close();
        }
        if (playwright != null) {
            playwright.close();
        }
    }

}
